#### Population, Business and Trade attitudes ####

#1) Data preparation ####

N <- 1000

seed <- 555

#1.1) set working directory ####

getwd() #Here in Survey_master.R sets the working directory to the project

#1.2) Load data ####

dat <- readRDS("data/dat.rds") %>%
  mutate(party_econ_cat = ifelse(party_econ_combined %in% c(0:2), "Left-wing",
                                 ifelse(party_econ_combined == 3, "Centre", 
                                        ifelse(party_econ_combined %in% c(4:6), "Right-wing", NA))),
         party_econ_combined = ifelse(party_econ_combined == 0, 1,
                                      ifelse(party_econ_combined == 6, 5,
                                             ifelse(is.na(party_econ_combined), NA, party_econ_combined))),
         party_cult_cat = ifelse(party_cult_combined %in% c(0:2), "Left-wing",
                                 ifelse(party_cult_combined == 3, "Centre", 
                                        ifelse(party_cult_combined %in% c(4:6), "Right-wing", NA))),
         party_cult_combined = ifelse(party_cult_combined == 0, 1,
                                      ifelse(party_cult_combined == 6, 5,
                                             ifelse(is.na(party_cult_combined), NA, party_cult_combined))),
         q2_treat = factor(q2_treat,
                           levels = c("Businesses support, population opposes",
                                      "Population support, businesses oppose"),
                           labels = c("Business", "Population")),
         q4_treat_actor = factor(q4_treat_actor,
                                 levels = c("Firms", "Workers"),
                                 labels = c("Business", "Population")),
         q5_treat = factor(q5_treat,
                           levels = c("Lower wages, increase profits",
                                      "Higher wages, reduce profits"),
                           labels = c("Business", "Population")),
         single = factor(ifelse(single_multiple_appointed %in% c("single", "signle"), "single", 
                                ifelse(single_multiple_appointed != "single", "multiple/appointed", NA))),
         university_education = ifelse(university_education == "Yes", "yes", university_education),
         trade_committee = ifelse(!is.na(Trade), "Yes", "No"),
         dist_high_edu = ifelse(msch >10, "Yes", "No"),
         trustees = (q6_populists + q6_unpopular + q6_technical)/3,
         delegates = (q6_demonstrations + q6_public + q6_elites)/3,
         del_trust = trustees - delegates,
         del_trust_cat = ifelse(del_trust < -.5, "Delegate",
                                ifelse(del_trust > .5, "Trustee", "Neutral")),
         gov_opp = factor(gov_opp_new,  #_alt
                          levels = c("opposition","government"),
                          labels = c("opposition", "government")),
         chamber_upper = factor(ifelse(parliament_chamber == "upper", "upper",
                                       ifelse(parliament_chamber %in% c("lower", "unicameral"), "lower/unicameral", NA))),
         lob_reg = factor(lob_reg,
                          levels = c("no", "yes"),
                          labels = c("No", "Yes")),
         types = factor(types,
                        levels = c(1:4),
                        labels = c("Left-Liberal", "Left-Cons", "Right-Liberal", "Right-Cons")),
         first_term = ifelse(grepl("for life", first_term), "no",
                             ifelse(grepl("NA", first_term), NA, first_term))) %>% 
  dplyr::select(-Trade)

# useful functions
'%!in%' <- function(x,y)!('%in%'(x,y))
std_error <- function(x) sd(x, na.rm=T)/sqrt(length(x[!is.na(x)]))


#1.3) Function to extract quantities of interest from bootstrapped models ####

pt <- function(mod, data=dat){
  
  df <- data
  
  mod <- bs(mod)
  
  if(class(mod) == "lmeresamp"){
    
    obj_mod <- mod
    out_mod <- mod$stats
    
    
    coef_names <- as.character(unlist(out_mod[,1]))
    est <- as.numeric(unlist(out_mod[,3]))
    se <- as.numeric(unlist(out_mod[,4]))
    
    #pval <- as.numeric()
    ci.low <- as.numeric()
    ci.up <- as.numeric()
    
    for (i in 1:ncol(obj_mod$replicates)){
      
      temp_repl <- as.numeric(unlist(c(obj_mod$replicates[,i])))
      
      ci.lowtemp = as.numeric(quantile(temp_repl, 0.025))
      ci.uptemp = as.numeric(quantile(temp_repl, 0.975))
            
      ci.low[i] <- ci.lowtemp
      ci.up[i] <- ci.uptemp
    
    }
    

    
    N <- nrow(obj_mod$data)
    
    gof <- c(N)
    gof_names <- c("N")
    gof_decimal <- c(F)
    
    texreg::createTexreg(coef.names = coef_names,
                         coef = est,
                         se = se,
                         #pvalues = pval,
                         ci.low = ci.low,
                         ci.up = ci.up,
                         gof = gof,
                         gof.names = gof_names,
                         gof.decimal = gof_decimal)
  }
}

# 1.4) Function to simplify bootsrapping ####

bs <- function(mod){
  set.seed(seed)
  
  bootstrap(mod, .f = lme4::fixef, type = "case", B = N,  resample = c(FALSE, TRUE))
  
}

#2) Main Effects ####
m2 <- lme4::lmer(q2 ~ q2_treat + (1|country_short), dat)
m4 <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction + (1|country_short), dat)
m5 <- lme4::lmer(q5 ~ q5_treat + (1|country_short), dat)

controls <- "+ age + gender + party_econ_combined + del_trust + gov_opp + single + chamber_upper + first_term + (1|country_short)"

m2c <- lme4::lmer(paste0("q2 ~ q2_treat", controls), dat)
m4c <- lme4::lmer(paste0("q4 ~ q4_treat_actor * q4_treat_direction", controls), dat)
m5c <- lme4::lmer(paste0("q5 ~ q5_treat", controls), dat)

# 2.1) Table C1 ####

texreg::texreg(list(pt(m2), pt(m5), pt(m4)),
               file = "tables/tableF1.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"), 
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Citizens, business, and PTA support",
               caption.above = T,
               single.row = F,
               label = "tab:tableF1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 2.1) Table F1 ####

texreg::texreg(list(pt(m2c), pt(m5c), pt(m4c)),
               file = "tables/tableI1.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "del_trust" = "Trustee vs. delegate",
                                      "gov_oppgovernment" = "Government party",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "first_termyes" = "First term legislator",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Robustness check: Table F1 with additional control variables",
               caption.above = T,
               single.row = F,
               label = "tab:tableI1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 2.3) Figure 1 ####

set.seed(seed)

marginaleffects::avg_slopes(m2, variables = "q2_treat", allow.new.levels = TRUE) %>% 
  marginaleffects::inferences(method = "boot", R = N)  %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 1",
         xlab = "") -> p1 

set.seed(seed)

marginaleffects::slopes(m4, variables = "q4_treat_actor", by = "q4_treat_direction", allow.new.levels = TRUE) %>% 
  marginaleffects::inferences(method = "boot", R = N) %>% 
            dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = q4_treat_direction) %>% 
            mutate(exp = "Experiment 3",
                   xlab = factor(xlab,
                                 levels = c("Benefit", "Loss"),
                                 labels = c("Benefit", "Loss"))) -> p2

set.seed(seed)

marginaleffects::avg_slopes(m5, variables = "q5_treat", allow.new.levels = TRUE) %>% 
  marginaleffects::inferences(method = "boot", R = N) %>%
            dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
            mutate(exp = "Experiment 2",
                   xlab = "") -> p3

bind_rows(p1,p2,p3) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_wrap(~exp, scales = "free_x") +
  theme(text = element_text(size = 10))

ggsave("figures/Figure1.pdf", width = 20, height = 8, units = "cm")
ggsave("figures/Figure1.png", width = 20, height = 8, units = "cm")

# 2.4) Figure I1 ####

set.seed(seed)

avg_slopes(m2, variables = "q2_treat", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N)  %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 1",
         controls = "No",
         xlab = "") -> p1

set.seed(seed)

slopes(m4, variables = "q4_treat_actor", by = "q4_treat_direction", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = q4_treat_direction) %>% 
  mutate(exp = "Experiment 3",
         controls = "No",
         xlab = factor(xlab,
                       levels = c("Benefit", "Loss"),
                       labels = c("Benefit", "Loss"))) -> p2

set.seed(seed)

avg_slopes(m5, variables = "q5_treat", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>%
  dplyr::select(AME = estimate,lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 2",
         controls = "No",
         xlab = "") -> p3

set.seed(seed)

avg_slopes(m2c, variables = "q2_treat", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N)  %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 1",
         controls = "Yes",
         xlab = "") -> p4

set.seed(seed)

slopes(m4c, variables = "q4_treat_actor", by = "q4_treat_direction", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = q4_treat_direction) %>% 
  mutate(exp = "Experiment 3",
         controls = "Yes",
         xlab = factor(xlab,
                       levels = c("Benefit", "Loss"),
                       labels = c("Benefit", "Loss"))) -> p5

set.seed(seed)

avg_slopes(m5c, variables = "q5_treat", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>%
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 2",
         controls = "Yes",
         xlab = "") -> p6

bind_rows(p1, p2, p3, p4, p5, p6) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = controls,
                shape = controls)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_wrap(~exp, scales = "free") +
  scale_colour_discrete("Are controls included?") + 
  scale_shape_discrete("Are controls included?") + 
  theme(text = element_text(size = 10),
        legend.position = "bottom")

ggsave("figures/FigureI1.pdf", width = 20, height = 10, units = "cm")


# 3) Interaction: political ideologies ####

m2i <- lme4::lmer(q2 ~ q2_treat * party_econ_combined + (1|country_short), dat)
m4i <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_econ_combined + (1|country_short), dat)
m5i <- lme4::lmer(q5 ~ q5_treat * party_econ_combined + (1|country_short), dat)

# 3.1) Table F2 ####

texreg::texreg(list(pt(m2i), pt(m5i), pt(m4i)),
               file = "tables/tableF2.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "party_econ_combined" = "Left-right ideology",
                                      "q2_treatPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q5_treatPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_actorPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_directionBenefit:party_econ_combined" = "Benefits $\\times$ Ideology",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:party_econ_combined" = "Citizens $\\times$ Benefits $\\times$ Ideology",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Citizens, business, left-right ideology, and PTA support",
               caption.above = T,
               single.row = F,
               label = "tab:tableF2", 
               custom.note = "\\parbox{0.9\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)



# 3.2) Figure 2 ####

m2i1 <- lme4::lmer(q2 ~ q2_treat * party_econ_cat + (1|country_short), dat)
m4i1 <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_econ_cat + (1|country_short), dat)
m5i1 <- lme4::lmer(q5 ~ q5_treat * party_econ_cat + (1|country_short), dat)

axis_pos <- dat %>% 
  group_by(party_econ_cat) %>% 
  summarise(pos = mean(party_econ_combined)) %>% 
  filter(!is.na(party_econ_cat)) %>% 
  arrange(pos)

set.seed(seed)

dat_bin <- slopes(m2i1, variables = "q2_treat", 
                  by = "party_econ_cat", 
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m2i, variables = "q2_treat", 
       by = "party_econ_combined", 
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo1

set.seed(seed)

dat_bin <- slopes(m4i1, variables = "q4_treat_actor", 
                  by = c("party_econ_cat", "q4_treat_direction"), 
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, direction = q4_treat_direction, party_econ_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m4i, variables = "q4_treat_actor",
       by = c("q4_treat_direction", "party_econ_combined"), 
              allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction,
                shape = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  geom_text(x=4, y=-2, label="Loss", colour = 2) + 
  geom_text(x=2.2, y=1.2, label="Benefit", colour = "#00BFC4") + 
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo3

set.seed(seed)

dat_bin <- slopes(m5i1, variables = "q5_treat", 
                  by = "party_econ_cat", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m5i, variables = "q5_treat", by = "party_econ_combined", allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "Left-right ideology (higher values: right-wing)", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo2

ggpubr::ggarrange(p_ideo1, p_ideo2, p_ideo3, nrow = 1)

ggsave("figures/Figure2.pdf", width = 27, height = 8, units = "cm")
ggsave("figures/Figure2.png", width = 27, height = 8, units = "cm")


# 4) Interaction: role conceptions - Support for Delegates vs Trustees ####

m2r <- lme4::lmer(q2 ~ q2_treat * del_trust_cat + (1|country_short), dat)
m4r <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * del_trust_cat + (1|country_short), dat)
m5r <- lme4::lmer(q5 ~ q5_treat * del_trust_cat +  (1|country_short), dat)

# 4.1) Table F3 ####

texreg::texreg(list(pt(m2r), pt(m5r), pt(m4r)),
               file = "tables/tableF3.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "del_trust_catNeutral" = "Role conceptions (Neutral)",
                                      "del_trust_catTrustee" = "Role conceptions (Trustee)",
                                      "q2_treatPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q2_treatPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q5_treatPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q5_treatPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q4_treat_actorPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q4_treat_actorPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q4_treat_directionBenefit:del_trust_catNeutral" = "Benefits $\\times$ Neutral role",
                                      "q4_treat_directionBenefit:del_trust_catTrustee" = "Benefits $\\times$ Trustee role",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catNeutral" = "Citizens $\\times$ Benefits $\\times$ Neutral role",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catTrustee" = "Citizens $\\times$ Benefits $\\times$ Trustee role",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Citizens, business, role conceptions, and PTA support",
               caption.above = T,
               single.row = F,
               label = "tab:tableF3", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Delegate role conceptions function as the reference category. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 4.2) Figure 3 ####

set.seed(seed)

marginaleffects::slopes(m2r,
                        variables = "q2_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_del1

set.seed(seed)

marginaleffects::slopes(m4r,
                        variables = "q4_treat_actor",
                        by = c("q4_treat_direction", "del_trust_cat"),
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high,
                colour = q4_treat_direction,
                shape = q4_treat_direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=2, y=0.6, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.25, y=-2, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") + 
    scale_shape_discrete("Treatment") -> p_del3

set.seed(seed)

marginaleffects::slopes(m5r,
                        variables = "q5_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none")  -> p_del2

ggpubr::ggarrange(p_del1, p_del2, p_del3, nrow = 1)

ggsave("figures/Figure3.pdf", width = 20, height = 8, units = "cm")
ggsave("figures/Figure3.png", width = 20, height = 8, units = "cm")

# 7) Appendix Section G: Predictions ####

# 7.1) Figure G1 ####

ann_text <- data.frame(xlab = c("Business", "Business"), estimate = c(7.5, 4.5), lower = c(7.5, 4), upper = c(7.5, 4), lab = c("Benefit", "Loss"),
                       exp = factor("Experiment 3",levels = c("Experiment 1","Experiment 2","Experiment 3")))

bind_rows(
  marginaleffects::avg_predictions(m2,
                                   variables = "q2_treat") %>% 
    dplyr::rename(xlab = 1) %>% 
    mutate(exp = "Experiment 1",
           xlab = factor(xlab, 
                         levels = c("Business", "Population"),
                         labels= c("P opposes\n B supports", "P supports\n B opposes")),
           lower = conf.low,
           upper = conf.high),
  marginaleffects::predictions(m4,
                               variables = "q4_treat_actor",
                               newdata = datagrid(q4_treat_direction = c("Benefit", "Loss"))) %>% 
    dplyr::rename(xlab = q4_treat_actor,  treatment = q4_treat_direction) %>% 
    mutate(exp = "Experiment 3",
           treatment = factor(treatment,
                              levels = c("Benefit", "Loss"),
                              labels = c("Benefit", "Loss")),
           lower = conf.low,
           upper = conf.high),
  marginaleffects::avg_predictions(m5,
                                   variables = "q5_treat") %>% 
    dplyr::rename(xlab = 1) %>% 
    mutate(exp = "Experiment 2",
           lower = conf.low,
           upper = conf.high,
           xlab = factor(xlab, 
                         levels = c("Business", "Population"),
                         labels = c("F win\n W lose", "W win\n F lose")))) %>% 
  ggplot(., aes(x = xlab,
                y = estimate,
                ymin = lower,
                ymax = upper)) + 
  geom_pointrange(position = position_dodge(width = .75)) +
  theme_minimal() +
  labs(x = "", y = "Predicted support for PTAs") +
  facet_wrap(~exp, scales = "free") +
  theme(text = element_text(size = 10),
        legend.position = "bottom") +
  scale_colour_discrete("Treatment") +
  scale_shape_discrete("Treatment") +
  geom_text(data = ann_text, aes(x = xlab, y = estimate, label = lab), position = "identity") +
  NULL

ggsave("figures/FigureG1.pdf", width = 20, height = 8, units = "cm")

# 7.2) Figure G2 ####

marginaleffects::predictions(m2i,
                             variables = "q2_treat",
                             newdata = datagrid(party_econ_combined = c(1:5))) %>% 
  rename(treatment = q2_treat,
         ideology = party_econ_combined) %>% 
  mutate(exp = "Experiment 1") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., aes(x = ideology,
                y = estimate,
                ymin = lower,
                ymax = upper,
                colour = treatment)) + 
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "Predicted support for PTAs") +
  facet_grid(~exp) +
  theme(legend.position = "bottom") +
  scale_colour_discrete("Treatment") +
  scale_x_continuous(breaks = c(0:6), minor_breaks = NULL) -> p_ideo1


marginaleffects::predictions(m4i,
                             variables = "q4_treat_actor",
                             newdata = datagrid(q4_treat_direction = c("Benefit", "Loss"),
                                                party_econ_combined = c(1:5)),
                             allow.new.levels = TRUE) %>% 
  rename(treatment = q4_treat_actor,
         direction = q4_treat_direction,
         ideology = party_econ_combined) %>% 
  mutate(exp = "Experiment 3") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., 
         aes(x = ideology,
             y = estimate,
             ymin = lower,
             ymax = upper,
             colour = treatment,
             shape = direction)) + 
  #geom_point() +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = c(0.8, 0.2)) +
  scale_x_continuous(breaks = c(0:6), minor_breaks = NULL) +
  geom_text(x=4, y=2.1, label="Loss", colour = "black") + 
  geom_text(x=3, y=9.25, label="Benefit", colour = "black") + 
  scale_colour_discrete("Treatment") +
  scale_shape_discrete("Direction") + guides(color = "none") -> p_ideo3

marginaleffects::predictions(m5i,
                             variables = "q5_treat",
                             newdata = datagrid(party_econ_combined = c(1:5))) %>% 
  rename(treatment = q5_treat,
         ideology = party_econ_combined) %>% 
  mutate(exp = "Experiment 2") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., aes(x = ideology,
                y = estimate,
                ymin = lower,
                ymax = upper,
                colour = treatment)) + 
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "Left-right ideology (higher values: right-wing)", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "bottom") +
  scale_x_continuous(breaks = c(0:6), minor_breaks = NULL)+
  scale_colour_discrete("Treatment") -> p_ideo2

ggpubr::ggarrange(p_ideo1, p_ideo2, p_ideo3, nrow = 1, legend = "bottom", common.legend = T)

ggsave("figures/FigureG2.pdf", width = 20, height = 8, units = "cm")


# 7.3) Figure G3 ####

marginaleffects::predictions(m2r,
                             by = c("q2_treat", "del_trust_cat")) %>% 
  rename(treatment = q2_treat,
         trustee = del_trust_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., aes(x = trustee,
                y = estimate,
                ymin = lower,
                ymax = upper,
                colour = treatment)) + 
  geom_pointrange(position = position_dodge(width = .75)) + 
  theme_minimal() +
  labs(x = "", y = "Predicted support for PTAs") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_colour_discrete("Treatment") -> p_del1

marginaleffects::predictions(m4r,
                             by = c("q4_treat_actor", "q4_treat_direction", "del_trust_cat")) %>% 
  
  rename(treatment = q4_treat_actor,
         direction = q4_treat_direction,
         trustee = del_trust_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., 
         aes(x = trustee,
             y = estimate,
             ymin = lower,
             ymax = upper,
             colour = treatment,
             shape = direction)) + 
  geom_pointrange(position = position_dodge(width = .75)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  #theme(legend.position = c(0.7, 0.4)) +
  geom_text(x=2.2, y=4, label="Loss", colour = "black") + 
  geom_text(x=2, y=6.5, label="Benefit", colour = "black") + 
  scale_colour_discrete("Treatment") +
  scale_shape_discrete("Direction") + 
  guides(color = "none") -> p_del3

marginaleffects::predictions(m5r,
                             by = c("q5_treat","del_trust_cat")) %>% 
  rename(treatment = q5_treat,
         trustee = del_trust_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  mutate(lower = conf.low,
         upper = conf.high) %>% 
  ggplot(., aes(x = trustee,
                y = estimate,
                ymin = lower,
                ymax = upper,
                colour = treatment)) + 
  geom_pointrange(position = position_dodge(width = .75)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_colour_discrete("Treatment") -> p_del2

ggpubr::ggarrange(p_del1, p_del2, p_del3, nrow = 1, legend = "bottom", common.legend = T)

ggsave("figures/FigureG3.pdf", width = 20, height = 8, units = "cm")

# 8) Appendix Section H: Alternative Operationalisation ####

# 8.1) Alternative operationalisation of political ideology ####

# 8.1.1) Cultural Left-right placement ####

m2ia <- lme4::lmer(q2 ~ q2_treat * party_cult_combined + (1|country_short), dat)
m4ia <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_cult_combined + (1|country_short), dat)
m5ia <- lme4::lmer(q5 ~ q5_treat * party_cult_combined + (1|country_short), dat)

# 8.1.1.1) Table H1 ####

texreg::texreg(list(pt(m2ia), pt(m5ia), pt(m4ia)),
               file = "tables/tableH1.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "party_cult_combined" = "Cultural ideology",
                                      "q2_treatPopulation:party_cult_combined" = "Citizens $\\times$ Ideology",
                                      "q5_treatPopulation:party_cult_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_actorPopulation:party_cult_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_directionBenefit:party_cult_combined" = "Benefits $\\times$ Ideology",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:party_cult_combined" = "Citizens $\\times$ Benefits $\\times$ Ideology",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F2 with alternative operationalisation of political ideology",
               caption.above = T,
               single.row = F,
               label = "tab:tableH1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 8.1.1.2) Figure H1 ####

m2ia1 <- lme4::lmer(q2 ~ q2_treat * party_cult_cat + (1|country_short), dat)
m4ia1 <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_cult_cat + (1|country_short), dat)
m5ia1 <- lme4::lmer(q5 ~ q5_treat * party_cult_cat + (1|country_short), dat)

axis_pos <- dat %>% 
  group_by(party_cult_cat) %>% 
  summarise(pos = mean(party_cult_combined)) %>% 
  filter(!is.na(party_cult_cat)) %>% 
  arrange(pos)

set.seed(seed)

dat_bin <- slopes(m2ia1, variables = "q2_treat", 
                  by = "party_cult_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_cult_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_cult_cat")

slopes(m2ia, variables = "q2_treat",
       by = "party_cult_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_cult_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo1

set.seed(seed)

dat_bin <- slopes(m4ia1, variables = "q4_treat_actor", 
                  by = c("party_cult_cat", "q4_treat_direction"),
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, direction = q4_treat_direction, party_cult_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_cult_cat")

set.seed(seed)

slopes(m4ia, variables = "q4_treat_actor",
       by = c("q4_treat_direction", "party_cult_combined"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, xlab = party_cult_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction,
                shape = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  geom_text(x=1.75, y=1.05, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.75, y=-.8, label="Loss", colour = 2) + 
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo3

set.seed(seed)

dat_bin <- slopes(m5ia1, variables = "q5_treat", 
                  by = "party_cult_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_cult_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_cult_cat")

set.seed(seed)

slopes(m5ia, variables = "q5_treat", by = "party_cult_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_cult_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "Left-right ideology (higher values: right-wing)", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo2

ggpubr::ggarrange(p_ideo1, p_ideo2, p_ideo3, nrow = 1)

ggsave("figures/FigureH1.pdf", width = 20, height = 8, units = "cm")

# 8.1.2) Types of parties #####

m2ia2 <- lme4::lmer(q2 ~ q2_treat * types + (1|country_short), dat)
m4ia2 <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * types + (1|country_short), dat)
m5ia2 <- lme4::lmer(q5 ~ q5_treat * types +  (1|country_short), dat)

# 8.1.2.1) Table H2 ####

texreg::texreg(list(pt(m2ia2), pt(m4ia2), pt(m5ia2)),
               file = "tables/tableH2.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "typesLeft-Cons" = "Left-Conservative",
                                      "typesRight-Liberal" = "Right-Liberal",
                                      "typesRight-Cons" = "Right-Conservative",
                                      "q2_treatPopulation:typesLeft-Cons" = "Population $\\times$ Left-Cons",
                                      "q2_treatPopulation:typesRight-Liberal" = "Population $\\times$ Right-Lib",
                                      "q2_treatPopulation:typesRight-Cons" = "Population $\\times$ Right-Cons",
                                      
                                      "q5_treatPopulation:typesLeft-Cons" = "Population $\\times$ Left-Cons",
                                      "q5_treatPopulation:typesRight-Liberal" = "Population $\\times$ Right-Lib",
                                      "q5_treatPopulation:typesRight-Cons" = "Population $\\times$ Right-Cons",
                                      
                                      "q4_treat_actorPopulation:typesLeft-Cons" = "Population $\\times$ Left-Cons",
                                      "q4_treat_actorPopulation:typesRight-Liberal" = "Population $\\times$ Right-Lib",
                                      "q4_treat_actorPopulation:typesRight-Cons" = "Population $\\times$ Right-Cons",
                                      
                                      "q4_treat_directionBenefit:typesLeft-Cons" = "Benefits $\\times$ Left-Cons",
                                      "q4_treat_directionBenefit:typesRight-Liberal" = "Benefits $\\times$ Right-Lib",
                                      "q4_treat_directionBenefit:typesRight-Cons" = "Benefits $\\times$ Right-Cons",
                                      
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:typesLeft-Cons" = "Citizens $\\times$ Benefits $\\times$ LC",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:typesRight-Liberal" = "Citizens $\\times$ Benefits $\\times$ RL",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:typesRight-Cons" = "Citizens $\\times$ Benefits $\\times$ RC",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F2 with alternative operationalisation of political ideology",
               caption.above = T,
               single.row = F,
               label = "tab:tableH2", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 8.1.2.2) Figure H2 ####

set.seed(seed)

marginaleffects::slopes(m2ia2,
                        variables = "q2_treat",
                        by = "types",
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = types,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) -> p_typ1

set.seed(seed)

marginaleffects::slopes(m4ia2,
                        variables = "q4_treat_actor",
                        by = c("q4_treat_direction", "types"),
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = types,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high,
                colour = q4_treat_direction,
                shape = q4_treat_direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  geom_text(x=3, y=1, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.25, y=-2, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") + 
  scale_shape_discrete("Treatment") -> p_typ3

set.seed(seed)

marginaleffects::slopes(m5ia2,
                        variables = "q5_treat",
                        by = "types",
                        allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = types,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "Ideological types", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))  -> p_typ2

ggpubr::ggarrange(p_typ1, p_typ2, p_typ3, nrow = 1)

ggsave("figures/FigureH2.pdf", width = 20, height = 10, units = "cm")

# 8.2) Trustee and Delegates individually ####

# 8.2.1) In a representative democracy, parliamentarians should act in the interest of the country even if that means making unpopular decisions ####

dat$q6_unpopular_cat <- factor(ifelse(dat$q6_unpopular %in% c(0:2), "Disagree/neutral",
                                      ifelse(dat$q6_unpopular %in% c(3), "Agree",
                                             ifelse(dat$q6_unpopular %in% c(4), "Strongly agree", NA))),
                               levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2unpop <- lme4::lmer(q2 ~ q2_treat * q6_unpopular_cat + (1|country_short), dat)
m4unpop <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_unpopular_cat + (1|country_short), dat)
m5unpop <- lme4::lmer(q5 ~ q5_treat * q6_unpopular_cat + (1|country_short), dat)

# 8.2.1.1) Table H3 ####

texreg::texreg(list(pt(m2unpop), pt(m5unpop), pt(m4unpop)),
               file = "tables/tableH3.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_unpopular_catAgree" = "Agree with T1",
                                      "q6_unpopular_catStrongly agree" = "Strongly agree with T1",
                                      "q2_treatPopulation:q6_unpopular_catAgree" = "Population $\\times$ Agree with T1",
                                      "q2_treatPopulation:q6_unpopular_catStrongly agree" = "Population $\\times$ Str. agree with T1",
                                      "q5_treatPopulation:q6_unpopular_catAgree" = "Population $\\times$ Agree with T1",
                                      "q5_treatPopulation:q6_unpopular_catStrongly agree" = "Population $\\times$ Str. agree with T1",
                                      "q4_treat_actorPopulation:q6_unpopular_catAgree" = "Population $\\times$ Agree with T1",
                                      "q4_treat_actorPopulation:q6_unpopular_catStrongly agree" = "Population $\\times$ Str. agree with T1",
                                      "q4_treat_directionBenefit:q6_unpopular_catAgree" = "Benefits $\\times$ Agree Unpopular",
                                      "q4_treat_directionBenefit:q6_unpopular_catStrongly agree" = "Benefits $\\times$ Str. agree with T1",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_unpopular_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with T1",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_unpopular_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with T1",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH3", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item T1 reads `Parliamentarians should act in the interest of the country even if that means making unpopular decisions.'}",
               use.packages = F)

# 8.1.1.2) Figure H3 ####

set.seed(seed)

slopes(m2unpop, variables = "q2_treat", by = "q6_unpopular_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_unpopular_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = q6_unpopular_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_unpop1

set.seed(seed)

slopes(m4unpop, 
       variables = "q4_treat_actor",
       by = c("q6_unpopular_cat", "q4_treat_direction"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_unpopular_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_unpopular_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction,
                shape = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  geom_text(x=1.65, y=.66, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.55, y=-1, label="Loss", colour = 2) + 
  theme(legend.position = "none") -> p_unpop2

set.seed(seed)

slopes(m5unpop, variables = "q5_treat", by = "q6_unpopular_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_unpopular_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_unpopular_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_unpop3

ggpubr::ggarrange(p_unpop1, p_unpop3, p_unpop2, nrow = 1)

ggsave("figures/FigureH3.pdf", width = 20, height = 9, units = "cm")

# 8.2.2) In a representative democracy, parliamentarians should act according to public opinion even if it is at odds with their own opinion ####

dat$q6_public_cat <- factor(ifelse(dat$q6_public %in% c(0:2), "Disagree/neutral",
                                      ifelse(dat$q6_public %in% c(3), "Agree",
                                             ifelse(dat$q6_public %in% c(4), "Strongly agree", NA))),
                               levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2publi <- lme4::lmer(q2 ~ q2_treat * q6_public_cat + (1|country_short), dat)
m4publi <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_public_cat + (1|country_short), dat)
m5publi <- lme4::lmer(q5 ~ q5_treat * q6_public_cat + (1|country_short), dat)

# 8.2.2.1) Table H4 ####

texreg::texreg(list(pt(m2publi), pt(m5publi), pt(m4publi)),
               file = "tables/tableH4.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_public_catAgree" = "Agree with D1",
                                      "q6_public_catStrongly agree" = "Str. agree with D1",
                                      "q2_treatPopulation:q6_public_catAgree" = "Population $\\times$ Agree with D1",
                                      "q2_treatPopulation:q6_public_catStrongly agree" = "Population $\\times$ Str. agree with D1",
                                      "q5_treatPopulation:q6_public_catAgree" = "Population $\\times$ Agree with D1",
                                      "q5_treatPopulation:q6_public_catStrongly agree" = "Population $\\times$ Str. agree with D1",
                                      "q4_treat_actorPopulation:q6_public_catAgree" = "Population $\\times$ Agree with D1",
                                      "q4_treat_actorPopulation:q6_public_catStrongly agree" = "Population $\\times$ Str. agree with D1",
                                      "q4_treat_directionBenefit:q6_public_catAgree" = "Benefits $\\times$ Agree with D1",
                                      "q4_treat_directionBenefit:q6_public_catStrongly agree" = "Benefits $\\times$ Str. agree with D1",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_public_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with D1",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_public_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with D1",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH4", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item D1 reads `Parliamentarians should act according to public opinion even if it is at odds with their own opinion.'}",
               use.packages = F)

# 8.2.2.2) Figure H4 ####

set.seed(seed)

slopes(m2publi, variables = "q2_treat", by = "q6_public_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_public_cat) %>% 
  mutate(exp = "Experiment 1") %>%
  ggplot(., aes(x = q6_public_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_public1

set.seed(seed)

slopes(m4publi, 
       variables = "q4_treat_actor",
       by = c("q6_public_cat", "q4_treat_direction"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_public_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_public_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=1.75, y=.5, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=2.2, y=-1.5, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") +
  NULL -> p_public3

set.seed(seed)

slopes(m5publi, variables = "q5_treat", by = "q6_public_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_public_cat)%>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_public_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "Parliamentarians should act according to public opinion even if it is at odds with their own opinion.", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_public2

ggpubr::ggarrange(p_public1, p_public2, p_public3, nrow = 1)

ggsave("./figures/FigureH4.pdf", width = 20, height = 9, units = "cm")

# 8.2.3) Demonstrations and letter-writing campaigns are legitimate ways to influence the decisions of parliamentarians. ####

dat$q6_demon_cat <- factor(ifelse(dat$q6_demonstrations %in% c(0:2), "Disagree/neutral",
                                   ifelse(dat$q6_demonstrations %in% c(3), "Agree",
                                          ifelse(dat$q6_demonstrations %in% c(4), "Strongly agree", NA))),
                            levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2demon <- lme4::lmer(q2 ~ q2_treat * q6_demon_cat + (1|country_short), dat)
m4demon <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_demon_cat + (1|country_short), dat)
m5demon <- lme4::lmer(q5 ~ q5_treat * q6_demon_cat + (1|country_short), dat)

# 8.2.3.1) Table H5 ####

texreg::texreg(list(pt(m2demon), pt(m5demon), pt(m4demon)),
               file = "tables/tableH5.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_demon_catAgree" = "Agree with D2",
                                      "q6_demon_catStrongly agree" = "Str. agree with D2",
                                      "q2_treatPopulation:q6_demon_catAgree" = "Population $\\times$ Agree with D2",
                                      "q2_treatPopulation:q6_demon_catStrongly agree" = "Population $\\times$ Str. agree with D2",
                                      "q5_treatPopulation:q6_demon_catAgree" = "Population $\\times$ Agree with D2",
                                      "q5_treatPopulation:q6_demon_catStrongly agree" = "Population $\\times$ Str. agree with D2",
                                      "q4_treat_actorPopulation:q6_demon_catAgree" = "Population $\\times$ Agree with D2",
                                      "q4_treat_actorPopulation:q6_demon_catStrongly agree" = "Population $\\times$ Str. agree with D2",
                                      "q4_treat_directionBenefit:q6_demon_catAgree" = "Benefits $\\times$ Agree with D2",
                                      "q4_treat_directionBenefit:q6_demon_catStrongly agree" = "Benefits $\\times$ Str. agree with D2",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_demon_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with D2",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_demon_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with D2",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH5", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item T2 reads `Demonstrations and letter-writing campaigns are legitimate ways to influence the decisions of parliamentarians.'}",
               use.packages = F)

# 8.2.3.2) Figure H5 ####

set.seed(seed)

slopes(m2demon, variables = "q2_treat", by = "q6_demon_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_demon_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = q6_demon_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_demo1

set.seed(seed)

slopes(m4demon, variables = "q4_treat_actor", by = c("q4_treat_direction", "q6_demon_cat"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_demon_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_demon_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=1.75, y=.3, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.35, y=-1.5, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") +
  NULL -> p_demo3

set.seed(seed)

slopes(m5demon, variables = "q5_treat", by = "q6_demon_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_demon_cat)  %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_demon_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "Demonstrations and letter-writing campaigns are legitimate ways to influence the decisions of parliamentarians.", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_demo2

ggpubr::ggarrange(p_demo1, p_demo2, p_demo3, nrow = 1)

ggsave("figures/FigureH5.pdf", width = 20, height = 9, units = "cm")

# 8.2.4) Trade agreements are so technical in nature that ordinary citizens cannot comprehend them. ####

dat$q6_techn_cat <- factor(ifelse(dat$q6_technical %in% c(0:2), "Disagree/neutral",
                                  ifelse(dat$q6_technical %in% c(3), "Agree",
                                         ifelse(dat$q6_technical %in% c(4), "Strongly agree", NA))),
                           levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2techn <- lme4::lmer(q2 ~ q2_treat * q6_techn_cat + (1|country_short), dat)
m4techn <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_techn_cat + (1|country_short), dat)
m5techn <- lme4::lmer(q5 ~ q5_treat * q6_techn_cat + (1|country_short), dat)

# 8.2.4.1) Table H6 ####

texreg::texreg(list(pt(m2techn), pt(m5techn), pt(m4techn)),
               file = "tables/tableH6.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_techn_catAgree" = "Agree with T2",
                                      "q6_techn_catStrongly agree" = "Str. agree with T2",
                                      "q2_treatPopulation:q6_techn_catAgree" = "Population $\\times$ Agree with T2",
                                      "q2_treatPopulation:q6_techn_catStrongly agree" = "Population $\\times$ Str. agree with T2",
                                      "q5_treatPopulation:q6_techn_catAgree" = "Population $\\times$ Agree with T2",
                                      "q5_treatPopulation:q6_techn_catStrongly agree" = "Population $\\times$ Str. agree with T2",
                                      "q4_treat_actorPopulation:q6_techn_catAgree" = "Population $\\times$ Agree with T2",
                                      "q4_treat_actorPopulation:q6_techn_catStrongly agree" = "Population $\\times$ Str. agree with T2",
                                      "q4_treat_directionBenefit:q6_techn_catAgree" = "Benefits $\\times$ Agree with T2",
                                      "q4_treat_directionBenefit:q6_techn_catStrongly agree" = "Benefits $\\times$ Str. agree with T2",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_techn_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with T2",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_techn_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with T2",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH6", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item T2 reads `Trade agreements are so technical in nature that ordinary citizens cannot comprehend them.'}",
               use.packages = F)

# 8.2.3.2) Figure H6 ####

set.seed(seed)

slopes(m2techn, variables = "q2_treat", by = "q6_techn_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_techn_cat) %>% 
  mutate(exp = "Experiment 1") %>%
  ggplot(., aes(x = q6_techn_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_techn1

set.seed(seed)

slopes(m4techn, 
       variables = "q4_treat_actor",
       by = c("q6_techn_cat", "q4_treat_direction"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_techn_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_techn_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=1.2, y=-1.3, label="Loss", colour = 2) + 
  geom_text(x=2, y=0.5, label="Benefit", colour = "#00BFC4") + 
  scale_colour_discrete("Treatment") +
  NULL -> p_techn3

set.seed(seed)

slopes(m5techn, variables = "q5_treat", by = "q6_techn_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_techn_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_techn_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "Trade agreements are so technical in nature that ordinary citizens cannot comprehend them.", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_techn2

ggpubr::ggarrange(p_techn1, p_techn2, p_techn3, nrow = 1)

ggsave("figures/FigureH6.pdf", width = 20, height = 9, units = "cm")

# 8.2.5) The influence of economic elites on trade negotiations is too big. ####

dat$q6_elite_cat <- factor(ifelse(dat$q6_elites %in% c(0:2), "Disagree/neutral",
                                  ifelse(dat$q6_elites %in% c(3), "Agree",
                                         ifelse(dat$q6_elites %in% c(4), "Strongly agree", NA))),
                           levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2elite <- lme4::lmer(q2 ~ q2_treat * q6_elite_cat + (1|country_short), dat)
m4elite <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_elite_cat + (1|country_short), dat)
m5elite <- lme4::lmer(q5 ~ q5_treat * q6_elite_cat + (1|country_short), dat)

# 8.2.5.1) Table H7 ####

texreg::texreg(list(pt(m2elite), pt(m5elite), pt(m4elite)),
               file = "tables/tableH7.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_elite_catAgree" = "Agree with D3",
                                      "q6_elite_catStrongly agree" = "Str. agree with D3",
                                      "q2_treatPopulation:q6_elite_catAgree" = "Population $\\times$ Agree with D3",
                                      "q2_treatPopulation:q6_elite_catStrongly agree" = "Population $\\times$ Str. agree with D3",
                                      "q5_treatPopulation:q6_elite_catAgree" = "Population $\\times$ Agree with D3",
                                      "q5_treatPopulation:q6_elite_catStrongly agree" = "Population $\\times$ Str. agree with D3",
                                      "q4_treat_actorPopulation:q6_elite_catAgree" = "Population $\\times$ Agree with D3",
                                      "q4_treat_actorPopulation:q6_elite_catStrongly agree" = "Population $\\times$ Str. agree with D3",
                                      "q4_treat_directionBenefit:q6_elite_catAgree" = "Benefits $\\times$ Agree with D3",
                                      "q4_treat_directionBenefit:q6_elite_catStrongly agree" = "Benefits $\\times$ Str. agree with D3",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_elite_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with D3",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_elite_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with D3",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH7", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item D3 reads `The influence of economic elites on trade negotiations is too big.'}",
               use.packages = F)

# 8.2.5.2) Figure H7 ####

set.seed(seed)

slopes(m2elite, variables = "q2_treat", by = "q6_elite_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_elite_cat) %>% 
  mutate(exp = "Experiment 1") %>%
  ggplot(., aes(x = q6_elite_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_elite1

set.seed(seed)

slopes(m4elite, 
       variables = "q4_treat_actor",
       by = c("q6_elite_cat", "q4_treat_direction"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_elite_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_elite_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=1.75, y=0.1, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=2.15, y=-1, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") +
  NULL -> p_elite3

set.seed(seed)

slopes(m5elite, variables = "q5_treat", by = "q6_elite_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_elite_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_elite_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "The influence of economic elites on trade negotiations is too big.", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_elite2

ggpubr::ggarrange(p_elite1, p_elite2, p_elite3, nrow = 1)

ggsave("figures/FigureH7.pdf", width = 20, height = 9, units = "cm")

# 8.2.6) Populists exploit the lack of knowledge of ordinary citizens about trade agreements. ####

dat$q6_popul_cat <- factor(ifelse(dat$q6_populists %in% c(0:2), "Disagree/neutral",
                                  ifelse(dat$q6_populists %in% c(3), "Agree",
                                         ifelse(dat$q6_populists %in% c(4), "Strongly agree", NA))),
                           levels = c("Disagree/neutral", "Agree", "Strongly agree"))

m2popul <- lme4::lmer(q2 ~ q2_treat * q6_popul_cat + (1|country_short), dat)
m4popul <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * q6_popul_cat + (1|country_short), dat)
m5popul <- lme4::lmer(q5 ~ q5_treat * q6_popul_cat + (1|country_short), dat)

# 8.2.6.1) Table H8 ####

texreg::texreg(list(pt(m2popul), pt(m5popul), pt(m4popul)),
               file = "tables/tableH8.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q6_popul_catAgree" = "Agree with T3",
                                      "q6_popul_catStrongly agree" = "Str. agree with T3",
                                      "q2_treatPopulation:q6_popul_catAgree" = "Population $\\times$ Agree with T3",
                                      "q2_treatPopulation:q6_popul_catStrongly agree" = "Population $\\times$ Str. agree with T3",
                                      "q5_treatPopulation:q6_popul_catAgree" = "Population $\\times$ Agree with T3",
                                      "q5_treatPopulation:q6_popul_catStrongly agree" = "Population $\\times$ Str. agree with T3",
                                      "q4_treat_actorPopulation:q6_popul_catAgree" = "Population $\\times$ Agree with T3",
                                      "q4_treat_actorPopulation:q6_popul_catStrongly agree" = "Population $\\times$ Str. agree with T3",
                                      "q4_treat_directionBenefit:q6_popul_catAgree" = "Benefits $\\times$ Agree with T3",
                                      "q4_treat_directionBenefit:q6_popul_catStrongly agree" = "Benefits $\\times$ Str. agree with T3",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_popul_catAgree" = "Citizens $\\times$ Benefits $\\times$ Agree with T3",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:q6_popul_catStrongly agree" = "Citizens $\\times$ Benefits $\\times$ Str. agree with T3",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F3 with alternative operationalisation of role conceptions",
               caption.above = T,
               single.row = F,
               label = "tab:tableH8", 
               custom.note = "\\parbox{0.95\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. Item T3 reads `Populists explot the lack of knowledge of ordinary citizens about trade agreements.'}",
               use.packages = F)

# 8.2.6.2) Figure H8 ####

set.seed(seed)

slopes(m2popul, variables = "q2_treat", by = "q6_popul_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_popul_cat) %>% 
  mutate(exp = "Experiment 1") %>%
  ggplot(., aes(x = q6_popul_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_popul1

set.seed(seed)

slopes(m4popul, 
       variables = "q4_treat_actor",
       by = c("q6_popul_cat", "q4_treat_direction"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, q6_popul_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = q6_popul_cat,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=1.7, y=.2, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.4, y=-1.2, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") +
  NULL -> p_popul3

set.seed(seed)

slopes(m5popul, variables = "q5_treat", by = "q6_popul_cat",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, q6_popul_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = q6_popul_cat,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) +
  theme_minimal() +
  labs(x = "Populists exploit the lack of knowledge of ordinary citizens about trade agreements.", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_popul2

ggpubr::ggarrange(p_popul1, p_popul2, p_popul3, nrow = 1)

ggsave("figures/FigureH8.pdf", width = 20, height = 9, units = "cm")

# 9) Appendix Section I: Additional controls ####

m2x <- lme4::lmer(paste0("q2 ~ q2_treat * (party_econ_combined + del_trust_cat)", controls), dat)
m4x <- lme4::lmer(paste0("q4 ~ q4_treat_actor * q4_treat_direction* (party_econ_combined + del_trust_cat)", controls), dat)
m5x <- lme4::lmer(paste0("q5 ~ q5_treat* (party_econ_combined + del_trust_cat)", controls), dat)

# 9.1) Table F2 ####

texreg::texreg(list(pt(m2x), pt(m5x), pt(m4x)),
               file = "tables/tableI2.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "q2_treatPopulation:party_econ_combined" = "Citizens $\\times$ L-R Ideology",
                                      "q5_treatPopulation:party_econ_combined" = "Citizens $\\times$ L-R Ideology",
                                      "q4_treat_actorPopulation:party_econ_combined" = "Citizens $\\times$ L-R Ideology",
                                      "q4_treat_directionBenefit:party_econ_combined" = "Benefits $\\times$ L-R Ideology",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:party_econ_combined" = "Citizens $\\times$ Benefits $\\times$ L-R Ideology",
                                      
                                      "q2_treatPopulation:del_trust_catNeutral" = "Citizens $\\times$ Role Perc.",
                                      "q5_treatPopulation:del_trust_catNeutral" = "Citizens $\\times$ Role Perc.",
                                      "q4_treat_actorPopulation:del_trust_catNeutral" = "Citizens $\\times$ Role Perc.",
                                      "q4_treat_directionBenefit:del_trust_catNeutral" = "Benefits $\\times$ Role Perc.",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catNeutral" = "Citizens $\\times$ Benefits $\\times$ Role Perc.",
                                      
                                      "q2_treatPopulation:del_trust_catTrustee" = "Citizens $\\times$ Role Perc.",
                                      "q5_treatPopulation:del_trust_catTrustee" = "Citizens $\\times$ Role Perc.",
                                      "q4_treat_actorPopulation:del_trust_catTrustee" = "Citizens $\\times$ Role Perc.",
                                      "q4_treat_directionBenefit:del_trust_catTrustee" = "Benefits $\\times$ Role Perc.",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catTrustee" = "Citizens $\\times$ Benefits $\\times$ Role Perc.",

                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "del_trust" = "Trustee vs. delegate",
                                      "lob_regYes" = "Strong lobbying regulation",
                                      "donation_banYes" = "Corporate donation ban",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "first_termyes" = "First term legislator",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: All interactions",
               caption.above = T,
               single.row = F,
               label = "tab:tableI2", 
               custom.note = "\\parbox{0.9\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 9.2) Figure I2 ####

m2x1 <- lme4::lmer(paste0("q2 ~ q2_treat * (party_econ_cat + del_trust_cat)", controls), dat)
m4x1 <- lme4::lmer(paste0("q4 ~ q4_treat_actor * q4_treat_direction* (party_econ_cat + del_trust_cat)", controls), dat)
m5x1 <- lme4::lmer(paste0("q5 ~ q5_treat* (party_econ_cat + del_trust_cat)", controls), dat)

axis_pos <- dat %>% 
  group_by(party_econ_cat) %>% 
  summarise(pos = mean(party_econ_combined)) %>% 
  filter(!is.na(party_econ_cat)) %>% 
  arrange(pos)

set.seed(seed)

dat_bin <- slopes(m2x1, variables = "q2_treat", 
                  by = "party_econ_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m2x, variables = "q2_treat", by = "party_econ_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo1

set.seed(seed)

dat_bin <- slopes(m4x1, variables = "q4_treat_actor", 
                  by = c("party_econ_cat", "q4_treat_direction"),
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, direction = q4_treat_direction, party_econ_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m4x, variables = "q4_treat_actor",
       by = c("q4_treat_direction", "party_econ_combined"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction,
                shape = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) + 
  geom_text(x=2.3, y=1, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=4, y=-2.1, label="Loss", colour = 2) + 
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo3

set.seed(seed)

dat_bin <- slopes(m5x1, variables = "q5_treat", 
                  by = "party_econ_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m5x, variables = "q5_treat", by = "party_econ_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "Left-right ideology (higher values: right-wing)", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo2

ggpubr::ggarrange(p_ideo1, p_ideo2, p_ideo3, nrow = 1)

ggsave("figures/FigureI2.pdf", width = 20, height = 8, units = "cm")

#9.3) Figure I3 ####

set.seed(seed)

marginaleffects::slopes(m2x,
                        variables = "q2_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_del1

set.seed(seed)

marginaleffects::slopes(m4x,
                        variables = "q4_treat_actor",
                        by = c("q4_treat_direction", "del_trust_cat"),
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high,
                colour = q4_treat_direction,
                shape = q4_treat_direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=2, y=0.6, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.2, y=-2, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") + 
  scale_shape_discrete("Treatment") -> p_del3

set.seed(seed)

marginaleffects::slopes(m5x,
                        variables = "q5_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none")  -> p_del2

ggpubr::ggarrange(p_del1, p_del2, p_del3, nrow = 1)

ggsave("figures/FigureI3.pdf", width = 20, height = 8, units = "cm")

# 10) Jackknifing ####

# 10.1) Figure J1 ####

country_list <- unique(dat$country_short)

jk_m <- data.frame(country_short = as.character(),
                   dv = as.character(),
                   iv = as.character(),
                   c = as.numeric(),
                   lower = as.numeric(),
                   upper = as.numeric())

for (i in country_list){
  
  set.seed(seed)
  
  dat_temp <- dat %>% filter(country_short != i)
  
  m2_temp <- lme4::lmer(q2 ~ q2_treat + (1|country_short), dat_temp) %>% 
    bs()
  m4_temp <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction + (1|country_short), dat_temp) %>% 
    bs()
  m5_temp <- lme4::lmer(q5 ~ q5_treat + (1|country_short), dat_temp) %>% 
    bs()
  
  jk_m <- rbind(jk_m,
                data.frame(country_short = i,
                           dv = c("Q2", "Q4", "Q4", "Q4", "Q5"),
                           iv = factor(c("Citizen", "Citizen", "Benefit", "Citizen \u00d7 Benefit", "Citizen"),
                                       levels = c("Citizen", "Benefit", "Citizen \u00d7 Benefit")),
                           c = c(m2_temp$stats$rep.mean[2], as.numeric(m4_temp$stats$rep.mean[2:4]), m5_temp$stats$rep.mean[2]),
                           lower = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.025))),
                           upper = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.975)))))
  
  
}

jk_m %>% 
  mutate(exp = factor(ifelse(dv == "Q2", "Experiment 1",
                             ifelse(dv == "Q5", "Experiment 2",
                                    ifelse(dv == "Q4", "Experiment 3", NA))))) %>% 
  ggplot(., aes(x=forcats::fct_rev(country_short), y=c)) +
  geom_pointrange(aes(ymin=lower, ymax=upper)) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1,size = 8)) +
  geom_hline(yintercept = 0, lty = "dotted") +
  ylab("Coefficient") +
  xlab("") +
  theme_minimal() + 
  coord_flip() + 
  ggh4x::facet_nested_wrap(~exp + iv, scales = "free_x",  nest_line = T, ncol = 5) + 
  NULL

ggsave("figures/FigureJ1.pdf", width = 20, height = 20, units = "cm")


# 10.2) Figure J2 ####

country_list <- unique(dat$country_short)

jk_m <- data.frame(country_short = as.character(),
                   dv = as.character(),
                   iv = as.character(),
                   c = as.numeric(),
                   lower = as.numeric(),
                   upper = as.numeric())

for (i in country_list){
  
  set.seed(seed)
  
  dat_temp <- dat %>% filter(country_short != i)
  
  m2_temp <- lme4::lmer(q2 ~ q2_treat * party_econ_combined + (1|country_short), dat_temp) %>% 
    bs()
  m4_temp <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_econ_combined + (1|country_short), dat_temp) %>% 
    bs()
  m5_temp <- lme4::lmer(q5 ~ q5_treat * party_econ_combined + (1|country_short), dat_temp) %>% 
    bs()
  
  jk_m <- rbind(jk_m,
                data.frame(country_short = i,
                           dv = rep(c("Q2", "Q4", "Q5"),c(3,7,3)),
                           iv = factor(c("Citizen", "Political Ideology", "Citizen \u00d7 Ideo", "Citizen", "Benefit", "Political Ideology", "Citizen \u00d7 Benefit", "Citizen \u00d7 Ideo", "Benefit \u00d7 Ideo", "C \u00d7 B \u00d7 I", "Citizen", "Political Ideology", "Citizen \u00d7 Ideo"),
                                       levels = c("Citizen", "Benefit", "Political Ideology", "Citizen \u00d7 Benefit", "Citizen \u00d7 Ideo", "Benefit \u00d7 Ideo", "C \u00d7 B \u00d7 I")),
                           c = c(m2_temp$stats$rep.mean[2:4], as.numeric(m4_temp$stats$rep.mean[2:8]), m5_temp$stats$rep.mean[2:4]),
                           lower = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,4]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,5]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,6]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,7]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,8]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,4]), 0.025))),
                           upper = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,4]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,5]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,6]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,7]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,8]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,4]), 0.975)))))
  
  
}

jk_m %>% 
  mutate(exp = factor(ifelse(dv == "Q2", "Experiment 1",
                             ifelse(dv == "Q5", "Experiment 2",
                                    ifelse(dv == "Q4", "Experiment 3", NA))))) %>% 
  filter(exp != "Experiment 3") %>% 
  ggplot(., aes(x=forcats::fct_rev(country_short), y=c)) +
  geom_pointrange(aes(ymin=lower, ymax=upper)) +

  theme(axis.text.x = element_text(angle = 60, hjust = 1,size = 8)) +
  geom_hline(yintercept = 0, lty = "dotted") +
  ylab("Coefficient") +
  xlab("") +
  theme_minimal() + 
  coord_flip() + 
  ggh4x::facet_nested_wrap(~exp + iv, scales = "free_x",  nest_line = T, ncol = 6) + 
  scale_y_continuous(guide = guide_axis(check.overlap = TRUE)) +
  NULL -> jkp1

jk_m %>% 
  mutate(exp = factor(ifelse(dv == "Q2", "Experiment 1",
                             ifelse(dv == "Q5", "Experiment 2",
                                    ifelse(dv == "Q4", "Experiment 3", NA))))) %>% 
  filter(exp == "Experiment 3") %>% 
  ggplot(., aes(x=forcats::fct_rev(country_short), y=c)) +
  geom_pointrange(aes(ymin=lower, ymax=upper)) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1,size = 8)) +
  geom_hline(yintercept = 0, lty = "dotted") +
  ylab("Coefficient") +
  xlab("") +
  theme_minimal() + 
  coord_flip() + 
  ggh4x::facet_nested_wrap(~exp + iv, scales = "free_x",  nest_line = T, ncol = 7) + 
  scale_y_continuous(guide = guide_axis(check.overlap = TRUE)) + 
  NULL -> jkp2

ggpubr::ggarrange(jkp1, jkp2, nrow = 2)

ggsave("figures/FigureJ2.pdf", width = 25, height = 30, units = "cm")


# 10.3) Figure J3 ####

country_list <- unique(dat$country_short)

jk_m <- data.frame(country_short = as.character(),
                   dv = as.character(),
                   iv = as.character(),
                   c = as.numeric(),
                   lower = as.numeric(),
                   upper = as.numeric())

for (i in country_list){
  
  set.seed(seed)
  
  dat_temp <- dat %>% filter(country_short != i)
  
  m2_temp <- lme4::lmer(q2 ~ q2_treat * del_trust + (1|country_short), dat_temp) %>% 
    bs()
  m4_temp <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * del_trust + (1|country_short), dat_temp) %>% 
    bs()
  m5_temp <- lme4::lmer(q5 ~ q5_treat * del_trust + (1|country_short), dat_temp) %>% 
    bs()
  
  jk_m <- rbind(jk_m,
                data.frame(country_short = i,
                           dv = rep(c("Q2", "Q4", "Q5"),c(3,7,3)),
                           iv = factor(c("Citizen", "Role perception", "Citizen \u00d7 Role", "Citizen", "Benefit", "Role perception", "Citizen \u00d7 Benefit", "Citizen \u00d7 Role", "Benefit \u00d7 Role", "C \u00d7 B \u00d7 R", "Citizen", "Role perception", "Citizen \u00d7 Role"),
                                       levels = c("Citizen", "Benefit", "Role perception", "Citizen \u00d7 Benefit", "Citizen \u00d7 Role", "Benefit \u00d7 Role", "C \u00d7 B \u00d7 R")),
                           c = c(m2_temp$stats$rep.mean[2:4], as.numeric(m4_temp$stats$rep.mean[2:8]), m5_temp$stats$rep.mean[2:4]),
                           lower = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,4]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,5]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,6]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,7]), 0.025)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,8]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,3]), 0.025)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,4]), 0.025))),
                           upper = c(as.numeric(quantile(unlist(m2_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m2_temp$replicates[,4]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,4]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,5]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,6]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,7]), 0.975)),
                                     as.numeric(quantile(unlist(m4_temp$replicates[,8]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,2]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,3]), 0.975)),
                                     as.numeric(quantile(unlist(m5_temp$replicates[,4]), 0.975)))))
  
  
}


jk_m %>% 
  mutate(exp = factor(ifelse(dv == "Q2", "Experiment 1",
                             ifelse(dv == "Q5", "Experiment 2",
                                    ifelse(dv == "Q4", "Experiment 3", NA))))) %>% 
  filter(exp != "Experiment 3") %>% 
  ggplot(., aes(x=forcats::fct_rev(country_short), y=c)) +
  geom_pointrange(aes(ymin=lower, ymax=upper)) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1,size = 8)) +
  geom_hline(yintercept = 0, lty = "dotted") +
  ylab("Coefficient") +
  xlab("") +
  theme_minimal() + 
  coord_flip() + 
  ggh4x::facet_nested_wrap(~exp + iv, scales = "free_x",  nest_line = T, ncol = 6) + 
  scale_y_continuous(guide = guide_axis(check.overlap = TRUE)) +
  NULL -> jkp1

jk_m %>% 
  mutate(exp = factor(ifelse(dv == "Q2", "Experiment 1",
                             ifelse(dv == "Q5", "Experiment 2",
                                    ifelse(dv == "Q4", "Experiment 3", NA))))) %>% 
  filter(exp == "Experiment 3") %>% 
  ggplot(., aes(x=forcats::fct_rev(country_short), y=c)) +
  geom_pointrange(aes(ymin=lower, ymax=upper)) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1,size = 8)) +
  geom_hline(yintercept = 0, lty = "dotted") +
  ylab("Coefficient") +
  xlab("") +
  theme_minimal() + 
  coord_flip() + 
  ggh4x::facet_nested_wrap(~exp + iv, scales = "free_x",  nest_line = T, ncol = 7) + 
  scale_y_continuous(guide = guide_axis(check.overlap = TRUE)) + 
  NULL -> jkp2

ggpubr::ggarrange(jkp1, jkp2, nrow = 2)

ggsave("figures/FigureJ3.pdf", width = 25, height = 30, units = "cm")

# 11) No upper chapter ####

# 11.1) Main Effects ####

m2nu <- lme4::lmer(q2 ~ q2_treat + (1|country_short), dat, subset = chamber_upper != "upper")
m4nu <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction + (1|country_short), dat, subset = chamber_upper != "upper")
m5nu <- lme4::lmer(q5 ~ q5_treat + (1|country_short), dat, subset = chamber_upper != "upper")

# 11.1.1) Table H1 ####

texreg::texreg(list(pt(m2nu), pt(m5nu), pt(m4nu)),
               file = "tables/tableK1.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F1 without upper chambers",
               caption.above = T,
               single.row = F,
               label = "tab:tableK1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 11.1.2) Figure K1 ####

set.seed(seed)

avg_slopes(m2nu, variables = "q2_treat",
           allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N)  %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 1",
         xlab = "") -> p1 

set.seed(seed)

slopes(m4nu, variables = "q4_treat_actor", by = "q4_treat_direction",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = q4_treat_direction) %>% 
  mutate(exp = "Experiment 3",
         xlab = factor(xlab,
                       levels = c("Benefit", "Loss"),
                       labels = c("Benefit", "Loss"))) -> p2

set.seed(seed)

avg_slopes(m5nu, variables = "q5_treat",
           allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>%
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high) %>% 
  mutate(exp = "Experiment 2",
         xlab = "") -> p3

bind_rows(p1,p2,p3) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_wrap(~exp, scales = "free_x") +
  theme(text = element_text(size = 10))

ggsave("figures/FigureK1.pdf", width = 20, height = 8, units = "cm")

# 11.2) Interaction: political ideologies ####

m2inu <- lme4::lmer(q2 ~ q2_treat * party_econ_combined + (1|country_short), dat, subset = chamber_upper != "upper")
m4inu <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_econ_combined + (1|country_short), dat, subset = chamber_upper != "upper")
m5inu <- lme4::lmer(q5 ~ q5_treat * party_econ_combined + (1|country_short), dat, subset = chamber_upper != "upper")

# 11.2.1) Table H2 ####

texreg::texreg(list(pt(m2inu), pt(m5inu), pt(m4inu)),
               file = "tables/tableK2.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "party_econ_combined" = "Left-right ideology",
                                      "q2_treatPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q5_treatPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_actorPopulation:party_econ_combined" = "Citizens $\\times$ Ideology",
                                      "q4_treat_directionBenefit:party_econ_combined" = "Benefits $\\times$ Ideology",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:party_econ_combined" = "Citizens $\\times$ Benefits $\\times$ Ideology",
                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "del_trust" = "Role conceptions (Trustee vs. delegate)",
                                      "donation_banYes" = "Corporate donation ban",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F2 without upper chambers",
               caption.above = T,
               single.row = F,
               label = "tab:tableK2", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 11.2.2) Figure K2 ####

m2inu1 <- lme4::lmer(q2 ~ q2_treat * party_econ_cat + (1|country_short), dat, subset = chamber_upper != "upper")
m4inu1 <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * party_econ_cat + (1|country_short), dat, subset = chamber_upper != "upper")
m5inu1 <- lme4::lmer(q5 ~ q5_treat * party_econ_cat + (1|country_short), dat, subset = chamber_upper != "upper")

axis_pos <- dat %>% 
  group_by(party_econ_cat) %>% 
  summarise(pos = mean(party_econ_combined)) %>% 
  filter(!is.na(party_econ_cat)) %>% 
  arrange(pos)

set.seed(seed)

dat_bin <- slopes(m2inu1, variables = "q2_treat", 
                  by = "party_econ_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 1") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m2inu, variables = "q2_treat", by = "party_econ_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo1

set.seed(seed)

dat_bin <- slopes(m4inu1, variables = "q4_treat_actor", 
                  by = c("party_econ_cat", "q4_treat_direction"),
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, direction = q4_treat_direction, party_econ_cat) %>% 
  mutate(exp = "Experiment 3") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m4inu, variables = "q4_treat_actor",
       by = c("q4_treat_direction", "party_econ_combined"),
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, direction = q4_treat_direction, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper,
                colour = direction,
                shape = direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  geom_text(x=2.3, y=1.2, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=4, y=-2.1, label="Loss", colour = 2) + 
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo3

set.seed(seed)

dat_bin <- slopes(m5inu1, variables = "q5_treat", 
                  by = "party_econ_cat",
                  allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(bin_AME = estimate, bin_lower = conf.low, bin_upper = conf.high, party_econ_cat) %>% 
  mutate(exp = "Experiment 2") %>% 
  dplyr::left_join(., axis_pos %>% 
                     dplyr::rename(xlab = pos),
                   by = "party_econ_cat")

set.seed(seed)

slopes(m5inu, variables = "q5_treat", by = "party_econ_combined",
       allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  dplyr::select(AME = estimate, lower = conf.low, upper = conf.high, xlab = party_econ_combined) %>% 
  ggplot(., aes(x = xlab,
                y = AME,
                ymin = lower,
                ymax = upper)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_errorbar(data = dat_bin, aes(y = bin_AME, ymin = bin_lower, ymax=bin_upper), lty = "dashed", width=0.2, position = position_dodge(width = .5)) +
  geom_line() + 
  geom_line(aes(y = lower), lty = "dashed") +
  geom_line(aes(y = upper), lty = "dashed") +
  theme_minimal() +
  labs(x = "Left-right ideology (higher values: right-wing)", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  scale_x_continuous(breaks = c(1:5)) -> p_ideo2

ggpubr::ggarrange(p_ideo1, p_ideo2, p_ideo3, nrow = 1)

ggsave("figures/FigureK2.pdf", width = 20, height = 8, units = "cm")

# 10.3) Interaction: role conceptions - Support for Delegates vs Trustees ####

m2rnu <- lme4::lmer(q2 ~ q2_treat * del_trust_cat + (1|country_short), dat, subset = chamber_upper != "upper")
m4rnu <- lme4::lmer(q4 ~ q4_treat_actor * q4_treat_direction * del_trust_cat + (1|country_short), dat, subset = chamber_upper != "upper")
m5rnu <- lme4::lmer(q5 ~ q5_treat * del_trust_cat + (1|country_short), dat, subset = chamber_upper != "upper")

# 10.3.1) Table K3 ####

texreg::texreg(list(pt(m2rnu), pt(m5rnu), pt(m4rnu)),
               file = "tables/tableK3.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "del_trust_catNeutral" = "Role conceptions (Neutral)",
                                      "del_trust_catTrustee" = "Role conceptions (Trustee)",
                                      "q2_treatPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q2_treatPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q5_treatPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q5_treatPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q4_treat_actorPopulation:del_trust_catNeutral" = "Population $\\times$ Neutral role",
                                      "q4_treat_actorPopulation:del_trust_catTrustee" = "Population $\\times$ Trustee role",
                                      "q4_treat_directionBenefit:del_trust_catNeutral" = "Benefits $\\times$ Neutral role",
                                      "q4_treat_directionBenefit:del_trust_catTrustee" = "Benefits $\\times$ Trustee role",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catNeutral" = "Citizens $\\times$ Benefits $\\times$ Neutral role",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit:del_trust_catTrustee" = "Citizens $\\times$ Benefits $\\times$ Trustee role",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Robustness check: Table F2 without upper chambers",
               caption.above = T,
               single.row = F,
               label = "tab:tableK3", 
               custom.note = "\\parbox{0.9\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 10.3.2) Figure K3 ####

set.seed(seed)

marginaleffects::slopes(m2rnu,
                        variables = "q2_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 1") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "Marginal effect: Citizens vs. business") +
  facet_grid(~exp) +
  theme(legend.position = "none") -> p_del1

set.seed(seed)

marginaleffects::slopes(m4rnu,
                        variables = "q4_treat_actor",
                        by = c("q4_treat_direction", "del_trust_cat"),
                        allow.new.levels = TRUE) %>%  
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 3") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high,
                colour = q4_treat_direction,
                shape = q4_treat_direction)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange(position = position_dodge(width = 1)) + 
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none") +
  geom_text(x=2, y=0.6, label="Benefit", colour = "#00BFC4") + 
  geom_text(x=1.5, y=-2.75, label="Loss", colour = 2) + 
  scale_colour_discrete("Treatment") + 
  scale_shape_discrete("Treatment") -> p_del3

set.seed(seed)

marginaleffects::slopes(m5rnu,
                        variables = "q5_treat",
                        by = "del_trust_cat",
                        allow.new.levels = TRUE) %>% 
  inferences(method = "boot", R = N) %>% 
  mutate(exp = "Experiment 2") %>% 
  ggplot(., aes(x = del_trust_cat,
                y = estimate,
                ymin = conf.low,
                ymax = conf.high)) + 
  geom_hline(yintercept = 0, lty = "dotted") +
  geom_pointrange() +
  theme_minimal() +
  labs(x = "", y = "") +
  facet_grid(~exp) +
  theme(legend.position = "none")  -> p_del2

ggpubr::ggarrange(p_del1, p_del2, p_del3, nrow = 1)

ggsave("figures/FigureK3.pdf", width = 20, height = 8, units = "cm")

# 11) Explaining role conceptions ####

dat$del_trust_cat2 <- factor(dat$del_trust_cat,
                             levels = c("Neutral", "Delegate", "Trustee"))

m_del <- nnet::multinom("del_trust_cat2 ~ age + gender + party_econ_combined + gov_opp + single + chamber_upper + country_short",
                        data = dat, Hess = T)

# 11.1) Table L1 ####

texreg::texreg(m_del,
               file = "tables/tableL1.tex",
               custom.model.names = c("Delegate", "Trustee"),
               omit.coef = "country_short",
               custom.coef.map = list("age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "gov_oppgovernment" = "Government party",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               caption = "Predictions of trustee/delegate preferences",
               ci.force = T, 
               caption.above = T,
               single.row = F,
               beside = T,
               label = "tab:tableL1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a multinomial regression model. Coefficients for countries have been omitted. Confidence intervals in parentheses. `Neutral' functions as a reference category for the outcome variable.}",
               use.packages = F)

# 12) Country fixed effects ####

m2cfe <- lm(q2 ~ q2_treat + country_short, dat)
m4cfe <- lm(q4 ~ q4_treat_actor * q4_treat_direction + country_short, dat)
m5cfe <- lm(q5 ~ q5_treat + country_short, dat)

controls <- "+ age + gender + party_econ_combined + del_trust + gov_opp + single + chamber_upper + first_term"

m2cfec <- lm(paste0("q2 ~ q2_treat", controls), dat)
m4cfec <- lm(paste0("q4 ~ q4_treat_actor * q4_treat_direction", controls), dat)
m5cfec <- lm(paste0("q5 ~ q5_treat", controls), dat)

texreg::screenreg(list(m2cfe, m2cfec, m4cfe, m4cfec, m5cfe, m5cfec),
                  omit.coef = "country_short")

# 12.1) Table M1 ####

texreg::texreg(list(m2cfe, m5cfe, m4cfe),
               file = "tables/tableM1.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"), 
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Citizens, business, and PTA support with country-fixed effects", 
               caption.above = T,
               single.row = F,
               label = "tab:tableM1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear regression model with country-fixed effects. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

# 12.2) Table M2 ####

texreg::texreg(list(m2cfec, m5cfec, m4cfec),
               file = "tables/tableM2.tex",
               custom.model.names = c("Experiment 1", "Experiment 2", "Experiment 3"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treatPopulation" = "Citizens",
                                      "q5_treatPopulation" = "Citizens",
                                      "q4_treat_actorPopulation" = "Citizens",
                                      "q4_treat_directionBenefit" = "Benefits",
                                      "q4_treat_actorPopulation:q4_treat_directionBenefit" = "Citizens $\\times$ Benefits",
                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "del_trust" = "Trustee vs. delegate",
                                      "gov_oppgovernment" = "Government party",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "first_termyes" = "First term legislator",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Robustness check: Table F1 with additional control variables and country-fixed effects",
               caption.above = T,
               single.row = F,
               label = "tab:tableM2", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale.}",
               use.packages = F)

#13) Individual treatment arms ####

dat$q2_treat_raw <- factor(dat$q2_treat_raw,
                           levels = c("q2a", "q2b", "q2c", "q2d"),
                           labels = c("Businesses support, population opposes",
                                      'Population opposes, businesses support',
                                      "Businesses oppose, population support",
                                      "Population support, businesses oppose"))

table(dat$q2_treat_raw)

dat$q5_treat_raw <- factor(dat$q5_treat_raw,
                           levels = c("q5a", "q5b", "q5c", "q5d"),
                           labels = c("lower wages, increased profit",
                                      "increased profit, lower wages",
                                      "higher wages, reduced profit",
                                      "reduced profit, higher wages"))

table(dat$q5_treat_raw)

controls <- "+ age + gender + party_econ_combined + del_trust + gov_opp + single + chamber_upper + first_term + (1|country_short)"

m2ta <- lme4::lmer(q2 ~ q2_treat_raw + (1|country_short), dat)
m2cta <- lme4::lmer(paste0("q2 ~ q2_treat_raw", controls), dat)

m5ta <- lme4::lmer(q5 ~ q5_treat_raw + (1|country_short), dat)
m5cta <- lme4::lmer(paste0("q5 ~ q5_treat_raw", controls), dat)

# 13.1) Table N1 ####

texreg::texreg(list(pt(m2ta), pt(m2cta)),
               file = "tables/tableN1.tex",
               custom.model.names = c("Experiment 1", "Experiment 1"),
               omit.coef = "country_short",
               custom.coef.map = list("q2_treat_rawPopulation opposes, businesses support" = "Population opposed, business support",
                                      "q2_treat_rawBusinesses oppose, population support" = "Business opposed, population support",
                                      "q2_treat_rawPopulation support, businesses oppose" = "Population support, business opposed",
                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "del_trust" = "Trustee vs. delegate",
                                      "gov_oppgovernment" = "Government party",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "first_termyes" = "First term legislator",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Robustness check: Experiment 1 with individual treatment conditions",
               caption.above = T,
               single.row = F,
               label = "tab:tableN1", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. ``Businesses support, population oppose'' is the reference category.}",
               use.packages = F)

# 13.2) Table N2 ####

texreg::texreg(list(pt(m5ta), pt(m5cta)),
               file = "tables/tableN2.tex",
               custom.model.names = c("Experiment 1", "Experiment 1"),
               omit.coef = "country_short",
               custom.coef.map = list("q5_treat_rawincreased profit, lower wages" = "Increased profit, lower wages",
                                      "q5_treat_rawhigher wages, reduced profit" = "Higher wages, reduced profit",
                                      "q5_treat_rawreduced profit, higher wages" = "Reduced profit, higher wages",
                                      "age" = "Age",
                                      "gendermale" = "Male legislator", 
                                      "party_econ_combined" = "Left-right ideology",
                                      "del_trust" = "Trustee vs. delegate",
                                      "gov_oppgovernment" = "Government party",
                                      "singlesingle" = "Single member districts",
                                      "chamber_upperupper" = "Upper chamber",
                                      "first_termyes" = "First term legislator",
                                      "(Intercept)" = "Intercept"),
               float = "htb",
               ci.force = T,
               caption = "Robustness check: Experiment 2 with individual treatment conditions",
               caption.above = T,
               single.row = F,
               label = "tab:tableN2", 
               custom.note = "\\parbox{0.85\\linewidth}{$^*$ Null hypothesis value outside the 95\\% confidence interval. Entries are unstandardized coefficients from a linear mixed-effect regression model. Bootstrapped confidence intervals in parentheses. Countries function as groups. Support for PTAs is captured on an 11-point scale. ``Lower wages, increased profit'' is the reference category.}",
               use.packages = F)


# 14) Descriptive statics ####

# 14.1) Table D1 ####

dat %>% 
  dplyr::select(q2, q5, q4,
                q2_treat, 
                q5_treat,
                q4_treat_actor, 
                q4_treat_direction,
                age, gender,
                party_econ_combined,
                del_trust_cat,
                gov_opp, 
                first_term) %>% 
  mutate(q2_treat = ifelse(is.na(q2), NA,
                           ifelse(q2_treat == "Population", 1, 0)),
         q4_treat_actor = ifelse(is.na(q4), NA,
                                 ifelse(q4_treat_actor == "Population", 1, 0)),
         q4_treat_direction = ifelse(is.na(q4), NA,
                                     ifelse(q4_treat_direction == "Benefit", 1, 0)),
         q5_treat = ifelse(is.na(q5), NA,
                           ifelse(q5_treat == "Population", 1, 0)),
         gender = ifelse(gender == "male", 1, 0),
         del = ifelse(del_trust_cat == "Delegate", 1,0),
         neu = ifelse(del_trust_cat == "Neutral", 1,0),
         tru = ifelse(del_trust_cat == "Trustee", 1,0),
         gov_opp = ifelse(gov_opp == "government", 1, 0),
         first_term = ifelse(first_term == "yes", 1, 0)) %>% 
  select(q2, q5, q4,
         q2_treat, 
         q5_treat,
         q4_treat_actor, 
         q4_treat_direction,
         age, gender,
         party_econ_combined,
         del, neu, tru,
         gov_opp,
         first_term) %>% 
  data.frame() %>% 
  stargazer::stargazer(covariate.labels = c("Support for trade (Experiment 1)",
                                            "Support for trade (Experiment 2)",
                                            "Support for trade (Experiment 3)",
                                            "Experiment 1: Citizens treatment",
                                            "Experiment 2: Citizens treatment",
                                            "Experiment 3: Citizens treatment",
                                            "Experiment 3: Benefit Treatment",
                                            "Age",
                                            "Male legislator",
                                            "Left-right ideology",
                                            "Preference for trustee",
                                            "Indifferent for trustee vs delegate",
                                            "Preference for delegate",
                                            "Government status",
                                            "First term legislator"), 
                       out = "tables/tableD1.tex", title = "Descriptive statistics", label = "tab:tableD1")


# 14.2) Figure E1 #####

population <- readRDS("data/population.RDS")

gps <- readxl::read_excel("data/Global Party Survey by Party SPSS V2_1_Apr_2020-1.xlsx") %>%
  dplyr::rename(party_econ = V4_Scale,
                party_cult = V6_Scale)

population <- population %>%
  dplyr::left_join(., gps %>% 
                     dplyr::select("Country", "Partyname", "party_econ", "party_cult"),
                   by = c("country" = "Country", "party_name_vparty" = "Partyname")) %>%
  filter(!is.na(party_econ)) %>%
  mutate(party_econ = ifelse(party_econ <= 2, "Far left\n(0-2)", 
                             ifelse(party_econ <= 4, "Left\n(2-4)", 
                                    ifelse(party_econ <= 6, "Center\n(4-6)", 
                                           ifelse(party_econ <= 8, "Right\n(6-8)", 
                                                  ifelse(party_econ <= 10, "Far right\n(8-10)", NA))))),
         party_econ1 = factor(party_econ,
                              levels = c("Far left\n(0-2)", "Left\n(2-4)", "Center\n(4-6)", "Right\n(6-8)", "Far right\n(8-10)")))

population %>%
  summarize(total = n(),
            respondents = sum(!is.na(q1))) %>%
  mutate(country_share = respondents / total)

population %>%
  group_by(country_short) %>%
  summarize(total = n(),
            respondents = sum(!is.na(last_page))) %>%
  mutate(country_share = respondents / total) %>% 
  ggplot(., aes(x = country_short, y = country_share)) +
  geom_bar(colour = "black", fill = "white", position = "stack", stat = "identity") + 
  ggthemes::theme_tufte() +
  scale_x_discrete(name = "Country") + 
  scale_y_continuous(name = "Response rate", labels = scales::percent) +
  theme(text = element_text(size = 9),
        axis.text.x = element_text(angle = 90, vjust=0.5)) +
  NULL

ggsave(filename = "./Figures/FigureE1.pdf",
       width = 17, height = 10, units = "cm")

# 11.3) Figure E2 ####

bind_rows(population %>% 
            filter(!is.na(gender)) %>%
            group_by(gender) %>%
            dplyr::summarize(total = n(),
                             respondents = sum(!is.na(last_page))) %>%
            mutate(share = respondents / total,
                   dim = "Gender") %>% 
            rename("variable" = gender),
          population %>% 
            filter(!is.na(gov_opp)) %>%
            group_by(gov_opp) %>%
            dplyr::summarize(total = n(),
                             respondents = sum(!is.na(last_page))) %>%
            mutate(share = respondents / total,
                   dim = "Government status") %>% 
            rename("variable" = gov_opp),
          population %>% 
            filter(!is.na(chamber_upper)) %>%
            group_by(chamber_upper) %>%
            dplyr::summarize(total = n(),
                             respondents = sum(!is.na(last_page))) %>%
            mutate(share = respondents / total,
                   dim = "Upper vs. lower chamber") %>% 
            rename("variable" = chamber_upper),
          population %>%
            filter(!is.na(party_econ)) %>%
            group_by(party_econ) %>%
            dplyr::summarize(total = n(),
                             respondents = sum(!is.na(last_page))) %>%
            mutate(share = respondents / total,
                   dim = "Econ. left-right position",
                   party_econ = factor(party_econ,
                                       levels = c("Far left\n(0-2)",
                                                  "Left\n(2-4)",
                                                  "Center\n(4-6)",
                                                  "Right\n(6-8)",
                                                  "Far right\n(8-10)"))) %>% 
            rename("variable" = party_econ)) %>% 
  mutate(dim = factor(dim,
                      levels = c("Gender", "Government status", "Upper vs. lower chamber", "Econ. left-right position"))) %>% 
  ggplot(., aes(x = variable, y = share)) +
  geom_bar(colour = "black", fill = "white", position = "stack", stat = "identity") + 
  ggthemes::theme_tufte() +
  scale_x_discrete(name = "Gender") + 
  scale_y_continuous(name = "Response rate", labels = scales::percent) +
  theme(text = element_text(size = 9)) +
  facet_wrap(dim~., nrow = 2,scales = "free_x") + 
  NULL


ggsave(filename = "figures/FigureE2.pdf", 
       width = 17,
       height = 10, units = "cm")


# 14.4) Figure E3 ####

share_female <- population %>% 
  filter(!is.na(gender)) %>%
  summarise(share_female_overall = mean(gender == "female"),
            share_female_responded = mean(gender == "female" & !is.na(last_page))
            / mean(!is.na(last_page)))  %>% 
  pivot_longer(
    everything(),
    names_to = "group",
    values_to = "share"
  ) %>%
  mutate(group = recode(group,
                        share_female_overall = "Overall",
                        share_female_responded = "Responded"))

pf <- ggplot(share_female, aes(x = group, y = share, fill = group)) +
  geom_col(width = 0.6) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(
    x = NULL,
    y = "Share female") +
  theme_minimal() +
  theme(legend.position = "none")

share_gov <- population %>% 
  filter(!is.na(gov_opp)) %>%
  summarise(share_gov_overall = mean(gov_opp == "government"),
            share_gov_responded = mean(gov_opp == "government" & !is.na(last_page))
            / mean(!is.na(last_page)))  %>% 
  pivot_longer(
    everything(),
    names_to = "group",
    values_to = "share"
  ) %>%
  mutate(group = recode(group,
                        share_gov_overall = "Overall",
                        share_gov_responded = "Responded"))

pg <- ggplot(share_gov, aes(x = group, y = share, fill = group)) +
  geom_col(width = 0.6) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(
    x = NULL,
    y = "Share government") +
  theme_minimal() +
  theme(legend.position = "none")

share_lower <- population %>% 
  filter(!is.na(chamber_upper)) %>%
  summarise(share_lower_overall = mean(chamber_upper == "lower/unicameral"),
            share_lower_responded = mean(chamber_upper == "lower/unicameral" & !is.na(last_page))
            / mean(!is.na(last_page))) %>% 
  pivot_longer(
    everything(),
    names_to = "group",
    values_to = "share"
  ) %>%
  mutate(group = recode(group,
                        share_lower_overall = "Overall",
                        share_lower_responded = "Responded"))

pl <- ggplot(share_lower, aes(x = group, y = share, fill = group)) +
  geom_col(width = 0.6) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(
    x = NULL,
    y = "Share lower chamber") +
  theme_minimal() +
  theme(legend.position = "none")

share_ideology <- population %>%
  filter(!is.na(party_econ)) %>%
  mutate(group = ifelse(!is.na(last_page), "Responded", "Overall")) %>%
  group_by(group, party_econ) %>%
  summarise(n = n()) %>%
  mutate(share = n / sum(n)) %>%
  ungroup() %>% 
  mutate(party_econ = factor(party_econ,
                      levels = c("Far left\n(0-2)",
                                 "Left\n(2-4)",
                                 "Center\n(4-6)",
                                 "Right\n(6-8)",
                                 "Far right\n(8-10)")))

pi <- ggplot(share_ideology, aes(x = party_econ, y = share, fill = group)) +
  geom_col(position = position_dodge(width = 0.8)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(
    x = "",# Economic ideology",
    y = "Share economic ideology",
    fill = "Group") +
  theme_minimal() +
  theme(axis.text.x = element_text(hjust = 1),
        legend.position = c(.1,.9))


ggpubr::ggarrange(pf, pg, pl, pi, ncol = 2, nrow = 2)
ggsave("figures/FigureE3.pdf", width = 17, height = 10)


# 14.5) Table D2 Description of role conceptions by gender  ####

n_del <- dat %>% select(gender, del_trust_cat) %>% drop_na %>% 
  group_by(gender) %>% 
  summarise(n_gender = n())

dat %>% 
  select(gender, del_trust_cat) %>% 
  drop_na() %>% 
  group_by(gender, del_trust_cat) %>% 
  summarise(n = n()) %>% 
  dplyr::left_join(., n_del, by = "gender") %>% 
  mutate(share = 100*n/n_gender,
         gender = factor(gender,
                         levels = c("female", "male"),
                         labels = c("Female", "Male"))) %>% 
  select(Gender = gender, `Role Perception` = del_trust_cat, N = n, `Share in %` = share) %>% 
  xtable::xtable(caption = "Role conception by gender", label = "tab:TableD2") %>% 
  print(., file = "tables/TableD2.tex", include.rownames=FALSE,caption.placement = "top")

# 14.6) Table D3. Description of role conceptions by ideology  ####

n_del <- dat %>% select(party_econ_cat, del_trust_cat) %>% drop_na %>% 
  group_by(party_econ_cat) %>% 
  summarise(n_party_econ_cat = n())

dat %>% 
  select(party_econ_cat, del_trust_cat) %>% 
  drop_na() %>% 
  group_by(party_econ_cat, del_trust_cat) %>% 
  summarise(n = n()) %>% 
  dplyr::left_join(., n_del, by = "party_econ_cat") %>% 
  mutate(share = 100*n/n_party_econ_cat,
         party_econ_cat = factor(party_econ_cat,
                         levels = c("Left-wing", "Centre", "Right-wing"))) %>% 
  arrange(party_econ_cat) %>% 
  select(`LR ideology` = party_econ_cat, `Role Perception` = del_trust_cat, N = n, `Share in %` = share) %>% 
  xtable::xtable(caption = "Role conception by political ideology", label = "tab:TableD3") %>% 
  print(., file = "tables/TableD3.tex", include.rownames=FALSE, caption.placement = "top")

# 14.7) Figure D1a-d ####

dat %>% 
  filter(!is.na(gender)) %>%
  mutate(gender = car::recode(gender, "'male' = 'Male'; 'female' = 'Female'")) %>%
  ggplot(., aes(x=gender)) +
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Gender") +
  theme(text = element_text(size = 12)) +
  NULL

ggsave(filename = "figures/FigureD1a.pdf",
       width = 17, height = 10, units = "cm")

dat %>% 
  filter(!is.na(years_parliament)) %>%
  ggplot(., aes(x=years_parliament)) +
  geom_histogram(colour = "black", fill = "white", bins = 10) +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Years since first term in parliament") +
  theme(text = element_text(size = 12)) +
  NULL

ggsave(filename = "figures/FigureD1b.pdf",
       width = 17, height = 10, units = "cm")

dat %>% 
  filter(!is.na(gov_opp)) %>%
  mutate(gov_opp = car::recode(gov_opp, "'government' = 'Government'; 'opposition' = 'Opposition'")) %>%
  ggplot(., aes(x=gov_opp)) +
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Government and opposition") +
  theme(text = element_text(size = 12)) +
  NULL

ggsave(filename = "figures/FigureD1c.pdf",
       width = 17, height = 10, units = "cm")

dat %>% 
  filter(!is.na(age)) %>%
  ggplot(., aes(x=age)) +
  geom_histogram(colour = "black", fill = "white", bins = 10) +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Age") +
  theme(text = element_text(size = 12)) +
  NULL

ggsave(filename = "figures/FigureD1d.pdf",
       width = 17, height = 10, units = "cm")
